package com.ysh.controller;

import com.github.pagehelper.Page;
import com.ysh.common.Code;
import com.ysh.common.PageResult;
import com.ysh.common.Result;
import com.ysh.pojo.User;
import com.ysh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 *  用户表现层
 */
@RestController
@RequestMapping("/user")
public class UserController {

    // 注入service
    @Autowired
    private UserService userService;

    /**
     *  新增用户
     * @param user
     * @return  Result<User>
     */
    @PostMapping
    public Result<User> add(@RequestBody User user){
        boolean result = userService.add(user);
        if (result){
            return new Result<User>(Code.OK,"添加成功！");
        }else{
            return new Result<User>(Code.ERROR,"新增失败！");
        }
    }

    /**
     *  删除单个用户
     * @param uuid 用户id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<User> delete(@PathVariable("id") Integer uuid){
        boolean result = userService.delete(uuid);
        if (result){
            return new Result<User>(Code.OK,"删除成功！");
        }else{
            return new Result<User>(Code.ERROR,"删除失败！");
        }

    }

    /**
     *  修改用户信息
     * @param user 封装用户对象
     * @return boolean
     */
    @PutMapping
    public Result<User> update(@RequestBody User user){
        boolean result = userService.update(user);
        if (result){
            return new Result<User>(Code.OK,"修改用户信息，成功！");
        }else{
            return new Result<User>(Code.ERROR,"修改用户信息，失败！");
        }
    }


    /**
     * 查询所有用户（带分页）
     * @param page 当前页
     * @param size 每页条数
     * @return Page<User>
     */
    @GetMapping("/{page}/{size}")
    public Result<User> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Page<User> list = userService.findAll(page, size);
        PageResult pageResult = new PageResult<User>(list.getTotal(),list.getPages(),list.getResult());
        return new Result(Code.OK, "分页查询所有用户，成功", pageResult);
    }

    /**
     *  查询单个用户
     * @param uuid 用户id
     * @return User user对象
     */
    @GetMapping("/{id}")
    public Result<User> findById(@PathVariable("id") Integer uuid){
        User user = userService.findById(uuid);
        Result<User> result = null;
        if (user != null){
            result = new Result<User>(Code.OK, "查询成功", user);
        }else{
            result = new Result<User>(Code.ERROR, "查询失败", user);
        }
        return result;
    }

    /**
     *  多条件模糊查询(带分页)
     * @param user 多条件封装成对象
     * @param page 当前页
     * @param size 每页条数
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result<User> getUsers(@RequestBody User user,@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        Page<User> pages = userService.getUsers(user,page,size);
        PageResult<User> pageResult = new PageResult<>(pages.getTotal(), pages.getPages(), pages.getResult());
        return new Result(Code.OK,"多条件模糊查询成功",pageResult);
    }

    /**
     *  用户登录
     * @param userName 用户名
     * @param password 密码
     * @return User 用户对象
     */
    @PostMapping("/login")
    public Result<User> login(String userName, String password){
        User user = userService.login(userName, password);
        Result<User> userResult = null;
        if (user != null){
            userResult  = new Result<>(Code.OK, "登录成功！",user);
        }else{
            userResult  = new Result<>(Code.ERROR, "登录失败！",user);
        }
        return userResult;

    }
}